package com.example.chuntiao.cophook.ui;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.example.chuntiao.cophook.R;
import com.example.chuntiao.cophook.adapter.PersonHiddenAdapter;
import com.example.chuntiao.cophook.beans.Person;
import com.example.chuntiao.cophook.beans.PersonDataSupport;
import com.example.chuntiao.cophook.beans.PersonHideDataSupport;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

public class HiddenPersonsActivity extends AppCompatActivity {
    PersonHiddenAdapter mAdapter;
    RecyclerView mRecView;

    int result = RESULT_CANCELED;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one_rec_basic);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setTitle("冷宫");
        mAdapter = new PersonHiddenAdapter();
        mRecView = (RecyclerView) findViewById(R.id.rec_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mRecView.setLayoutManager(layoutManager);
        mRecView.setAdapter(mAdapter);
        mAdapter.setOnCancelClickListener(new PersonHiddenAdapter.OnCancelClickListener() {
            @Override
            public void onClick(View v, int position) {
                ArrayList<Person> persons = mAdapter.getmPersons();
                final Person p = persons.get(position);
                persons.remove(p);
                mAdapter.notifyItemRemoved(position);
                mAdapter.notifyItemRangeChanged(0, persons.size());
                result = RESULT_OK;

                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        DataSupport.deleteAll(PersonHideDataSupport.class, "uid=?", p.getUid());
                        new PersonDataSupport(p).save();
                    }
                }.start();
            }
        });
    }

    @Override
    public void onBackPressed() {
        setResult(result);
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
        new LoadDataTask().execute();
    }


    class LoadDataTask extends AsyncTask<Void, Void, ArrayList<Person>> {

        @Override
        protected ArrayList<Person> doInBackground(Void... params) {

            List<PersonHideDataSupport> hiddenPersonsData = DataSupport.findAll(PersonHideDataSupport.class);
            ArrayList<Person> hiddenPersons = new ArrayList<>();

            for (PersonHideDataSupport p : hiddenPersonsData) {
                hiddenPersons.add(new Person(p));
            }
            hiddenPersonsData.clear();
            return hiddenPersons;
        }

        @Override
        protected void onPostExecute(ArrayList<Person> persons) {
            mAdapter.setmPersons(persons);
            mAdapter.notifyDataSetChanged();
        }
    }
}